package com.example.mq.config;

import com.example.mq.config.contant.StaticConstant;
import com.example.mq.config.mq.MqConsumerService;
import com.example.mq.interceptor.LoggerInterceptor;
import com.example.mq.property.EsProperties;
import com.example.mq.property.MqProperties;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.http.HttpHost;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.persistence.EntityManager;
import java.io.IOException;

@Configuration
public class SystemConfig implements WebMvcConfigurer {

    @Autowired
    public MqProperties properties;

    @Autowired
    public EsProperties esProperties;

    @Autowired
    public MqConsumerService mqConsumerService;

    @Autowired
    LoggerInterceptor loggerInterceptor;

    @Value("${spring.profiles.active}")
    private String profile;


    @Value("${mq.consumer.expression}")
    private String expression;

    @Bean
    public DefaultMQProducer producer() throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer(properties.getGroup());
        producer.setNamesrvAddr(properties.getRocketMqUrl());
        producer.start();
        return producer;
    }

    @Bean
    public DefaultMQPushConsumer consumer() throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(properties.getGroup());
        consumer.setNamesrvAddr(properties.getRocketMqUrl());
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        consumer.registerMessageListener(mqConsumerService);
        consumer.subscribe(properties.getTopic(), expression);
        consumer.start();
        return consumer;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if(StaticConstant.DEV.getName().equals(profile)){
            registry.addInterceptor(loggerInterceptor).excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg");

        }
    }

    @Bean(value = "client")
    public RestHighLevelClient restHighLevelClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost(esProperties.getHostname(), esProperties.getPort(), esProperties.getScheme()));
        return new RestHighLevelClient(builder);
    }

    @Bean(value = "queryFactory")
    //依赖其他的bean
    public JPAQueryFactory jpaQueryFactory(EntityManager entityManager, RestHighLevelClient client) throws IOException {
        System.out.println(client.indices().exists(new GetIndexRequest("32"), RequestOptions.DEFAULT));
        System.out.println(entityManager.toString());
        return new JPAQueryFactory(entityManager);
    }

}
